package com.dayuanit.dy15.lesson25.demo2;

import java.util.Random;

public class Test {

    public static void a(int num) {
        if (num > 10) {
            System.out.println("num=" + num);
            throw new NullPointerException("快看，控制啦");
        } else if (num > 5 && num <= 10) {
            System.out.println("num=" + num);
            throw new MyException("数字不允许在5-10之间");
        } else if (num == 2) {
            String xx = null;
            xx.toUpperCase();//空指针异常 NPE
        }
    }

    //在方法上 声明检查型异常，为什么要声明？警示调用者，必须要对此异常做处理，不可不闻不问。
    //
    public static void b(int num) throws MyExceptionChecked {
        if (num < 5) {
            System.out.println("num=" + num);
            throw new MyExceptionChecked();//检查型的异常
        }
    }


    public static void c() throws MyExceptionChecked{
        a(9);

        b(2);
    }

    public static void d() {
        try {
            c();
        } catch (MyExceptionChecked me) {
            me.printStackTrace();
        }
    }

    public static void e() {
        System.out.println("e() begin...");
        try {
            a(6);
        } catch (NullPointerException ne) {
            System.out.println("catch NullPointerException");
            ne.printStackTrace();
        } catch (MyException my) {
            System.out.println("catch MyException");
            my.printStackTrace();
        } catch (Exception e) {
            System.out.println("catch Exception");
            e.printStackTrace();
        }

        System.out.println("e() end...");
    }

    public static void main(String[] args) {
        Test.e();
        System.out.println("main end");
    }
}
